package com.yc.snacknet_vue.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yc.snacknet_vue.bean.CartInfo;
import com.yc.snacknet_vue.dao.DBHelper;
import com.yc.snacknet_vue.dao.ICartInfoDAO;

/**
 * 购物车 数据模型层 实现
 * @author UNO
 * @date 2021年8月10日
 * Email 2759451734@qq.com
 */
public class CartInfoDAOImpl implements ICartInfoDAO {

	/**
	 * 查询 某个会员的购物车信息
	 * @param mno
	 * @return
	 */
	@Override
	public List<CartInfo> finds(String mno) {
		DBHelper db = new DBHelper();
		String sql = "select cno, c.gno, num, price, pics, gname, unit, weight from goodsinfo g, cartinfo c where g.gno = c.gno and mno = ? ";
		return db.findMultiple(CartInfo.class, sql, mno);
	}

	/**
	 * 这个只查询当前用不了的购物车编号和商品编号
	 * @param mno
	 * @return
	 */
	@Override
	public List<Map<String, Object>> info(String mno) {
		DBHelper db = new DBHelper();
		String sql = "select cno, gno from cartinfo where mno = ? ";
		return db.findMultiple(sql, mno);
	}

	/**
	 * 修改购物车中商品数量
	 * @param cno
	 * @param num
	 * @return
	 */
	@Override
	public int update(int num, String cno) {
		DBHelper db = new DBHelper();
		String sql = "update cartinfo set num = num + ? where cno = ? ";
		return db.update(sql, num, cno);
	}

	/**
	 * 添加购物车信息
	 * @param cf
	 * @return
	 */
	@Override
	public int add(CartInfo cf) {
		DBHelper db = new DBHelper();
		String sql = "insert into cartinfo values(?, ?, ?, ?)";
		return db.update(sql, cf.getCno(), cf.getMno(), cf.getGno(), cf.getNum());
	}

	/**
	 * 删除指定购物车编号的购物信息
	 * @param cno
	 * @return
	 */
	@Override
	public int delete(String cno) {
		DBHelper db = new DBHelper();
		String sql =" delete from cartinfo where cno = ?";
		return db.update(sql, cno);
	}

	/**
	 * 删除指定多个购物车编号的购物信息
	 * @param cnos
	 * @return
	 */
	@Override
	public int deletes(String[] cnos) {
		DBHelper db = new DBHelper();
		String sql = "delete from cartinfo where cno in (";
		List<Object> params = new ArrayList<Object>();
		for( String cno : cnos) {   // ?,?,?
			sql += "?,"; //?,?,
			params.add(cno);
		}
		sql = sql.substring(0, sql.lastIndexOf(",")) + ")";  //( ?, ?)
		return db.update(sql, params);
	}

	/**
	 * 查询指定购物车编号数组的购物车信息
	 * @param cnos
	 * @return
	 */
	@Override
	public List<CartInfo> findByCno(String[] cnos) {
		DBHelper db = new DBHelper();
		String sql = "select cno, c.gno, num, price, pics, gname, unit, weight from goodsinfo g, cartinfo c where g.gno = c.gno and cno in ( ";
		List<Object> params = new ArrayList<Object>();
		for( String cno : cnos) {   // ?,?,?
			sql += "?,"; //?,?,
			params.add(cno);
		}
		sql = sql.substring(0, sql.lastIndexOf(",")) + ")";  //( ?, ?)
		return db.findMultiple(CartInfo.class, sql, params.toArray());
	}

}
